<!DOCTYPE html>

<html>
<head>
  <title>imaging.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          <h1>imaging.coffee</h1>
        

        
          <div class="toc">
            <h3>Table of Contents</h3>
            <ol>
              
                
                <li>
                  <a class="source" href="configure.html">
                    configure.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="database.html">
                    database.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="imaging.html">
                    imaging.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="logger.html">
                    logger.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mail.html">
                    mail.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="manager.html">
                    manager.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="routes.html">
                    routes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="security.html">
                    security.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settings.html">
                    settings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sockets.html">
                    sockets.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sync.html">
                    sync.coffee
                  </a>
                </li>
              
            </ol>
          </div>
        
      </div>

      
        
        <h2>SERVER IMAGING</h2>

        
      
        
        <p>Handles and manipulates images on the server.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Imaging</span></span></pre></div>
        
      
        
        <p>Define the filesystem and ImageMagick objects.</p>

        
          <div class='highlight'><pre>    fs = require <span class="string">"fs"</span>
    im = require <span class="string">"imagemagick"</span>
    logger = require <span class="string">"./logger.coffee"</span>
    settings = require <span class="string">"./settings.coffee"</span></pre></div>
        
      
        
        <p>Converts the specified SVG to PNG, by creating a new file with same name
but different extension. Image will also be resized and scale to the specified width.
A callback (err, result) can be passed as well.</p>

        
          <div class='highlight'><pre>    svgToPng: (svgSource, width, callback) =&gt;
        fs.exists svgSource, (exists) -&gt;
            <span class="keyword">if</span> exists
                <span class="keyword">try</span></pre></div>
        
      
        
        <p>Try converting the SVG to a PNG file, and trigger
the <code>callback</code> if one was passed.</p>

        
          <div class='highlight'><pre>                    im.convert [svgSource, <span class="string">"-resize"</span>, width, svgSource.replace(<span class="string">".svg"</span>, <span class="string">".png"</span>)]
                    <span class="keyword">if</span> settings.General.debug
                        logger.info <span class="string">"Imaging.svgToPng"</span>, svgSource, width
                    <span class="keyword">if</span> callback?
                        callback <span class="literal">null</span>, <span class="string">"ok"</span>

                <span class="keyword">catch</span> ex</pre></div>
        
      
        
        <p>In case of exception, log it and pass to the <code>callback</code>.</p>

        
          <div class='highlight'><pre>                    logger.error <span class="string">"Imaging.svgToPng"</span>, ex
                    <span class="keyword">if</span> callback?
                        callback ex, <span class="literal">null</span>

            <span class="keyword">else</span></pre></div>
        
      
        
        <p>SVG does not exist, so log the warning and trigger
the <code>callback</code> if one was passed.</p>

        
          <div class='highlight'><pre>                msg = <span class="string">"Can't convert SVG to PNG: <span class="subst">#{svgSource}</span> does not exist."</span>
                logger.warn <span class="string">"Imaging.svgToPng"</span>, msg
                <span class="keyword">if</span> callback?
                    callback msg, <span class="literal">null</span></pre></div>
        
      
        
        <h2>Singleton implementation</h2>

        
      
        
        
        
          <div class='highlight'><pre>Imaging.<span class="function"><span class="title">getInstance</span></span> = -&gt;
    <span class="property">@instance</span> = <span class="keyword">new</span> Imaging() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.exports = exports = Imaging.getInstance()</pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>
